package com.hisan.freeride.home.adapter;

import android.content.Intent;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.library.BaseQuickAdapter;
import com.github.library.BaseViewHolder;

import com.hisan.freeride.R;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.ConvertUtils;
import com.hisan.freeride.home.model.LookRelease;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * 查看预约乘客列表适配器
 * Created by yu on 2017/1/17.
 */

public class LookReserveAdapter extends BaseQuickAdapter<LookRelease,BaseViewHolder > {
    public LookReserveAdapter(List<LookRelease> data) {
        super(R.layout.look_release_item,data);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final LookRelease homeModel) {
        if (!CollectionUtils.isNullOrEmpty(homeModel.getPassenger_avatar())){
            SimpleDraweeView simpleDraweeView= holder.getView(R.id.logo);
            simpleDraweeView.setImageURI(homeModel.getPassenger_avatar());
        }
        holder.setText(R.id.name, homeModel.getPassenger_name());
        holder.setText(R.id.phone, homeModel.getPassenger_mobile());
        switch (homeModel.getOrder_state()){
            case 1:
                holder.setText(R.id.status,"已同意");
                holder.getView(R.id.select).setVisibility(View.GONE);
                holder.getView(R.id.ok).setOnClickListener(null);
                holder.getView(R.id.cancel).setOnClickListener(null);
                break;
            case 2:
                holder.setText(R.id.status,"已拒绝");
                holder.getView(R.id.select).setVisibility(View.GONE);
                holder.getView(R.id.ok).setOnClickListener(null);
                holder.getView(R.id.cancel).setOnClickListener(null);
                break;
            case 3:
                holder.setText(R.id.status,"已取消");
                holder.getView(R.id.select).setVisibility(View.GONE);
                holder.getView(R.id.ok).setOnClickListener(null);
                holder.getView(R.id.cancel).setOnClickListener(null);
                break;
                default:
                    String time=homeModel.getDeparture_time()+":00";
                    if (getTime(time)<0){
                        if (homeModel.getComment()==0){
                            holder.setText(R.id.status,"待审核");
                            holder.getView(R.id.select).setVisibility(View.VISIBLE);
                        }
                    }else {
                        holder.setText(R.id.status,"行程已过期");
                        holder.getView(R.id.select).setVisibility(View.GONE);
                        holder.getView(R.id.ok).setOnClickListener(null);
                        holder.getView(R.id.cancel).setOnClickListener(null);
                    }
                    break;
        }

        holder.getView(R.id.cancel).setOnClickListener(v -> cancel(homeModel));
        holder.getView(R.id.ok).setOnClickListener(v -> ok(homeModel));
        holder.getView(R.id.tel).setOnClickListener(v -> Phone(homeModel));
    }

    //取消用户订单
    private void cancel(LookRelease homeModel) {
        int id=homeModel.getId();
        Intent intent = new Intent("cancel");
        intent.putExtra("id", id);
        mContext.sendBroadcast(intent);
    }

    //确认用户订单
    private void ok(LookRelease homeModel) {
        int id=homeModel.getId();
        Intent intent = new Intent("ok");
        intent.putExtra("id", id);
        mContext.sendBroadcast(intent);
    }


    //拨打电话
    private void Phone(LookRelease homeModel) {
        String phone=homeModel.getPassenger_mobile();
        Intent intent = new Intent("phone");
        intent.putExtra("phone", phone);
        mContext.sendBroadcast(intent);
    }
    // 将字符串转为时间戳
    public  long getTime(String stime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate =new  Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        long st= ConvertUtils.timeDifference(stime,str);
        return st;
    }
}
